package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.DelayedConstant;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.service.OrderPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 普通自提订单,备货完成后，买家过期不来拿货，自动完成
 */
@Component
@Slf4j
public class AutoStockUpConsumer extends BaseConsumer {

    @Autowired
    private OrderService orderService;

    @Resource
    private OrderPackageService orderPackageService;

    @Resource
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;

    @Override
    protected void process(Message message) {
        Long orderId = ClassConvertUtils.objToLong(new String(message.getBody()));
        Order v = orderService.getById(orderId);
        try {
            log.info(String.format("普通消费自提订单:%s,开始执行自动备货", v.getOrderCode()));
            OrderPackage orderPackage = orderPackageService.autoPickupShip(v);
            sendOrderOrRightsOrderMsgService.pickupShipSuccessSendMsg(orderPackage);
            log.info(String.format("普通消费自提订单:%s,完成执行自动备货", v.getOrderCode()));
        }
        catch (Exception ex) {
            log.error(String.format("普通消费自提订单:%s,执行自动备货,出错%s", v.getOrderCode(), ex.getMessage()));
            throw  ex;
        }


    }

    @RabbitListener(queues = DelayedConstant.OrderAutoStockUpConstant.AUTO_STOCK_UP_DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message,channel);
    }
}
